package me.jessyan.mvparms.demo.mvp.ui.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DoubleClickUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.di.component.login.DaggerLoginAndRegsiterComponent;
import me.jessyan.mvparms.demo.di.module.login.LoginAndRegsiterModule;
import me.jessyan.mvparms.demo.mvp.contract.login.LoginAndRegsiterContract;
import me.jessyan.mvparms.demo.mvp.presenter.login.FindPassAndBindSetPresenter;
import me.jessyan.mvparms.demo.mvp.presenter.login.LoginAndRegsiterPresenter;
import me.jessyan.mvparms.demo.mvp.ui.activity.home.HomeActivity;
import me.jessyan.mvparms.demo.mvp.ui.widget.MyVerifyLinerText;
import me.jessyan.mvparms.demo.mvp.ui.widget.PassWordEditText;
import me.jessyan.mvparms.demo.mvp.ui.widget.PhoneEditText;
import me.jessyan.mvparms.demo.mvp.ui.widget.SizeChangRadioBn;

import static com.jess.arms.utils.Preconditions.checkNotNull;
import static me.jessyan.mvparms.demo.mvp.presenter.login.LoginAndRegsiterPresenter.PageStatus.PASSWORD;
import static me.jessyan.mvparms.demo.mvp.presenter.login.LoginAndRegsiterPresenter.PageStatus.REGISTER;
import static me.jessyan.mvparms.demo.mvp.presenter.login.LoginAndRegsiterPresenter.PageStatus.VERIFY;


public class LoginAndRegsiterActivity extends BaseActivity<LoginAndRegsiterPresenter> implements LoginAndRegsiterContract.View {


    @BindView(R.id.login_rb)
    SizeChangRadioBn loginRb;
    @BindView(R.id.register_rb)
    SizeChangRadioBn registerRb;
    @BindView(R.id.rp)
    RadioGroup rp;
    @BindView(R.id.phone_et)
    PhoneEditText phoneEt;
    @BindView(R.id.verify_ll)
    MyVerifyLinerText verifyLl;
    @BindView(R.id.asking_verify_tv)
    TextView askingVerifyTv;
    @BindView(R.id.change_passWord_tv)
    TextView changePassWordTv;
    @BindView(R.id.weiChat_iv)
    ImageView weiChatIv;
    @BindView(R.id.login_tv)
    TextView loginTv;
    @BindView(R.id.passWord_et)
    PassWordEditText passWordEt;
    @BindView(R.id.notice_tv)
    TextView noticeTv;
    @BindView(R.id.notice_frame)
    FrameLayout noticeFrame;
    @BindView(R.id.quick_ll)
    LinearLayout quickLl;


    /**
     * 输入框些的输入是否满足需求
     */
    private boolean phoneOk, verifyOk, passWordOk;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerLoginAndRegsiterComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .loginAndRegsiterModule(new LoginAndRegsiterModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_login_and_regsiter; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        loginRb.setChecked(true);
        registerRb.setChecked(false);
        initListener();
        LoginAndRegsiterPresenter.PageStatus status = VERIFY;
        mPresenter.ChangeCurrentStatus(status);


    }

    private void initListener() {
        // 默认登录是验证码
        rp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.login_rb) {
                    mPresenter.ChangeCurrentStatus(VERIFY);
                } else {
                    mPresenter.ChangeCurrentStatus(REGISTER);
                }
            }
        });
        // 手机号输入框
        phoneEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //当12位时向后又输入一位达到加上空格十三位
                if (s.length() == 13) {
                    phoneOk = true;
                } else {
                    phoneOk = false;
                }
                checkLoginTvClickable();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //验证码
        verifyLl.setVerifyListener(new MyVerifyLinerText.VerifyListener() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 4) {
                    verifyOk = true;
                } else {
                    verifyOk = false;
                }
                checkLoginTvClickable();
            }

            @Override
            public void onClick(View view) {
                verifyLl.startTimer();
            }
        });

        //密码输入框
        //password是否有输入
        passWordEt.setInputLisenter(new PassWordEditText.InputLisenter() {

            @Override
            public void inputChange(boolean isInput) {
                passWordOk = isInput;
                checkLoginTvClickable();
            }
        });


    }


    /**
     * 更新是否可点击状态
     */
    private void checkLoginTvClickable() {
        boolean enable = false;
        switch (mPresenter.getmCurrentStatus()) {

            case PASSWORD:
                if (phoneOk && passWordOk) {
                    enable = true;
                }
                break;
            case VERIFY:
                if (phoneOk && verifyOk) {
                    enable = true;
                }
                break;
            case REGISTER:
                if (phoneOk && verifyOk && passWordOk) {
                    enable = true;
                }
                break;
            default:
                enable = false;
        }
        loginTv.setEnabled(enable);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @OnClick({R.id.asking_verify_tv, R.id.change_passWord_tv, R.id.login_tv, R.id.weiChat_iv})
    public void onViewClicked(View view) {
        if (DoubleClickUtils.getInstance().isDouble(view)) {
            return;
        }
        switch (view.getId()) {
            case R.id.asking_verify_tv:
                    if(mPresenter.getmCurrentStatus()==VERIFY){
                        startActivity(new Intent(this,VerifyAnswerActivity.class));

                    }else {

                        Intent intent =new Intent(this,FindPassAndBindSetActivity.class);
                        intent.putExtra("page_type",me.jessyan.mvparms.demo.mvp.presenter.login.FindPassAndBindSetPresenter.FIND_PASSWORD);
                        startActivity(intent);
                    }
                break;
            case R.id.change_passWord_tv:
                if (mPresenter.getmCurrentStatus() == PASSWORD) {
                    mPresenter.ChangeCurrentStatus(VERIFY);
                } else if (mPresenter.getmCurrentStatus() == VERIFY) {
                    mPresenter.ChangeCurrentStatus(PASSWORD);
                }

                break;
            case R.id.login_tv:
                    startActivity(new Intent(LoginAndRegsiterActivity.this, HomeActivity.class));
                break;
            case R.id.weiChat_iv:
                Intent intent =new Intent(this,FindPassAndBindSetActivity.class);
                intent.putExtra("page_type", FindPassAndBindSetPresenter.VERIFY);
                startActivity(intent);
                break;
        }
    }



    @Override
    public void changeVerify() {
        loginTv.setText("登录");
        weiChatIv.setVisibility(View.VISIBLE);
        quickLl.setVisibility(View.VISIBLE);
        noticeTv.setVisibility(View.GONE);
        noticeFrame.setVisibility(View.VISIBLE);
        askingVerifyTv.setText("收不到验证码？");
        changePassWordTv.setText("密码登录");
        passWordEt.setVisibility(View.GONE);
        verifyLl.setVisibility(View.VISIBLE);
        checkLoginTvClickable();
    }
    @Override
    public void changePassWord() {
        loginTv.setText("登录");
        weiChatIv.setVisibility(View.VISIBLE);
        quickLl.setVisibility(View.VISIBLE);
        noticeTv.setVisibility(View.GONE);
        noticeFrame.setVisibility(View.VISIBLE);
        changePassWordTv.setText("验证码登录");
        askingVerifyTv.setText("忘记密码");
        passWordEt.setVisibility(View.VISIBLE);
        verifyLl.setVisibility(View.GONE);
        passWordEt.setHint("请输入密码");
        checkLoginTvClickable();
    }
    @Override
    public void changeRegister() {
        loginTv.setText("注册");
        weiChatIv.setVisibility(View.GONE);
        quickLl.setVisibility(View.GONE);
        noticeFrame.setVisibility(View.GONE);
        noticeTv.setVisibility(View.VISIBLE);
        passWordEt.setVisibility(View.VISIBLE);
        verifyLl.setVisibility(View.VISIBLE);
        passWordEt.setHint("请设置密码");
        checkLoginTvClickable();
    }


}
